# basicsfx.py
# Copyright (C) 2007 Sergio Perez Camacho - BRUE
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA


import pygame
from pygame.locals import *

class BaseSong:
    def __init__(self, fSong):
        self.beats=[]
        # Load the song
        print "[FreeBand] Loading " + fSong
        pygame.mixer.music.load(fSong)        
        self.loadsng(fSong[:-4])
        print self.bps
        print (1000/self.bps)
        print 429 % (1000/self.bps)
        
        
        
    def loadsng(self, fsng):
        # A very ugly and simple parser
        f = open(fsng+".sng", "r")        
        ind = 0
        l1 = []
        l2 = []
        l3 = []
        l4 = []
        l5 = []
        for line_ in f:
            line = line_[0:-1]
            if line == "[BPM]":
                ind=0
                continue
            if line == "[1]":
                ind=1
                continue
            if line == "[2]":
                ind=2
                continue
            if line == "[3]":
                ind=3
                continue
            if line == "[4]":
                ind=4
                continue
            if line == "[5]":
                ind=5
                continue
            print line
            if ind == 0:
                self.bpm = float(line)
                self.bps = self.bpm / 60
            elif ind == 1: l1.append(int(line))
            elif ind == 2: l2.append(int(line))
            elif ind == 3: l3.append(int(line))
            elif ind == 4: l4.append(int(line))
            elif ind == 5: l5.append(int(line))
        
        self.beats.append(l1)
        self.beats.append(l2)
        self.beats.append(l3)
        self.beats.append(l4)
        self.beats.append(l5)
        
        f.close()
        
    def play(self):
        pygame.mixer.music.play()
    
    def pause(self):
        pygame.mixer.music.pause()
        
    def stop(self):
        pygame.mixer.music.stop()


class SoundKit:    
    def __init__(self, sound_files):
        self.kit = []
        for file in sound_files:
             self.kit.append(pygame.mixer.Sound(file))
        
                
    def play(self, index):        
        if index in range(0, len(self.kit)):
            for c in channels:
                if not c.get_busy():
                    c.play(self.kit[index])
                    break
        else:
            print "No kit number avaiable"

        
    def stop(self, index):
        if index in range(0, len(self.kit)):
            self.kit[index].stop()
        else:
            print "No kit number avaiable"

def init_mixer(n):
    lchannels = []
    pygame.mixer.init()
    pygame.mixer.set_num_channels(n)
    print "[FreeBand] Initializing mixer channels to: " + str(n)
    for i in range(0,n):
        lchannels.append(pygame.mixer.Channel(i))
    return lchannels
        
def init():
    channels = init_mixer(20)
    
